package com.woniu.cms.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.cms.finance.core.entity.FinanceAccounting;
import com.woniu.cms.finance.core.entity.FinanceInvoiceIncome;
import com.woniu.cms.finance.core.mapper.FinanceInvoiceIncomeMapper;
import com.woniu.cms.finance.core.service.IFinanceAccountingService;
import com.woniu.cms.finance.core.service.IFinanceInvoiceIncomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.finance.core.vo.GetInvoiceVo;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 开票审批表 服务实现类
 * </p>
 *
 * @author kiron
 * @since 2022-09-19
 */
@Service
public class FinanceInvoiceIncomeServiceImpl extends ServiceImpl<FinanceInvoiceIncomeMapper, FinanceInvoiceIncome> implements IFinanceInvoiceIncomeService {

    @Autowired
    private FinanceInvoiceIncomeMapper financeInvoiceIncomeMapper;

    @Autowired
    private IFinanceAccountingService financeAccountingService;


    @Override
    public Result getList(String projectNum) {
        LambdaQueryWrapper<FinanceInvoiceIncome> qw = Wrappers.lambdaQuery();
        if(projectNum != null){
            qw.eq(FinanceInvoiceIncome::getProjectNum,projectNum);
        }
        List<FinanceInvoiceIncome> list = this.list(qw);
        Map map = new HashMap();
        map.put("data",list);
        Result<Map> common = Result.common(map);
        return common;
    }

    @Override
    public Result updateInvoice(FinanceInvoiceIncome financeInvoiceIncome) {
        if(ObjectUtils.isEmpty(financeInvoiceIncome)){
            return Result.error(BusinessCode.MODIFY_FAIL);
        }
        if(financeInvoiceIncome.getApprovalStatus()!=0&& financeInvoiceIncome.getApprovalStatus()!=3){
            return Result.error("发票审批已提交，不可修改");
        }
        LambdaUpdateWrapper<FinanceInvoiceIncome> updateWrapper = Wrappers.lambdaUpdate();
       updateWrapper.eq(FinanceInvoiceIncome::getId, financeInvoiceIncome.getId());
        boolean update = this.update(financeInvoiceIncome, updateWrapper);
        if (!update){
            return Result.error(BusinessCode.MODIFY_FAIL);
        }
        return Result.ok(BusinessCode.MODIFY_SUCCESS);
    }

    @Override
    public Result getAll(Integer currentPage, Integer pageSize) {
        Page<GetInvoiceVo> invoiceVos = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> financeInvoiceIncomeMapper.getAll());
        List<GetInvoiceVo> result = invoiceVos.getResult();
        long total = invoiceVos.getTotal();
        Map map = new HashMap();
        map.put("total",total);
        map.put("records",result);
        Result<Map> common = Result.common(map);
        return common;
    }

    @Override
    public Result saveInvoice(FinanceInvoiceIncome financeInvoiceIncome) {

        Integer id = financeInvoiceIncome.getAccountingId();
        FinanceAccounting financeAccounting = financeAccountingService.getById(id);
        //获取已开票金额
        BigDecimal invoiceCount = financeAccounting.getInvoiceCount();
        if (invoiceCount == null){
            BigDecimal bigDecimal=new BigDecimal(0);

            financeAccounting.setInvoiceCount(bigDecimal);
        }
        //获取累计列帐
        BigDecimal cumulativeAccounting = financeAccounting.getCumulativeAccounting();
        //获取本次开票审批金额
        BigDecimal invoiceCount1 = financeInvoiceIncome.getInvoiceCount();
        if(invoiceCount1.add(invoiceCount).compareTo(cumulativeAccounting) !=1){
            boolean save = this.save(financeInvoiceIncome);
            if(save){
                return Result.ok(BusinessCode.ADD_SUCCESS);
            }
            return Result.error(BusinessCode.ADD_FAIL);
        }


        return Result.error(BusinessCode.ADD_FAIL);
    }


}
